package org.chen.concurrent.springbootmybatisconcurrent.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.chen.concurrent.springbootmybatisconcurrent.dao.ProductsMapper;
import org.chen.concurrent.springbootmybatisconcurrent.domain.Products;
import org.chen.concurrent.springbootmybatisconcurrent.service.ProductsService;
import org.chen.concurrent.springbootmybatisconcurrent.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class ProductsServiceImpl implements ProductsService {

    private static final String REDIST_KEY = "redis:db:";

    @Autowired
    private RedisUtil redisUtil;

    @Resource
    private ProductsMapper productsMapper;

    @Value("${spring.redis.seconds}")
    private int seconds;

    //Redis集群调用
//    @Override
//    public Products queryById(int id) {
//        Products products = redisUtil.getCluster(REDIST_KEY + id, Products.class);
//        if (products != null) {
//            log.info("[Redis]中读取数据,{}", products);
//            return products;
//        } else {
//            log.info("[数据库]中读取数据,{}", products);
//            Products current = productsMapper.queryById(id);
//            redisUtil.setCluster(REDIST_KEY + current.getId(), current, seconds);
//            return current;
//        }
//    }


    @Override
    public Products queryById(int id) {
        Products current = productsMapper.queryById(id);
        log.info("[数据库]中读取数据,{}", current);
        return current;
    }
}
